<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="filters">
        <el-form-item>
          <el-input v-model="filters.keywords" placeholder="姓名"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="keywordsQuery">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" disabled @click="handleAdd">新增</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleExport">导出</el-button>
        </el-form-item>
        <el-form-item>
          <!-- 默认name="file" -->
          <el-upload class="upload-demo"
                     action="http://localhost:8080/shop/import"
                     list-type="text">
            <el-button type="warning">导入</el-button>
          </el-upload>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表-->
    <el-table :data="shops" highlight-current-row v-loading="listLoading" @selection-change="selsChange"
              style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column prop="name" label="店铺名字" width="120" sortable>
      </el-table-column>
      <el-table-column prop="tel" label="电话号码" width="120"  sortable>
      </el-table-column>
      <el-table-column prop="registerTime" label="注册时间" width="100" sortable>
      </el-table-column>
      <el-table-column prop="state" label="状态" width="80" sortable>
        <template scope="scope">
          <span style="color: #8c939d" v-if="scope.row.state==1">待审核</span>
          <span style="color: cornflowerblue" v-if="scope.row.state==2">待激活</span>
          <span style="color: #11b95c" v-if="scope.row.state==3">正常使用</span>
          <span style="color: #e64242" v-if="scope.row.state==4">审核驳回</span>
        </template>
      </el-table-column>
      <el-table-column prop="address" label="地址" min-width="120" sortable>
      </el-table-column>
      <el-table-column prop="logo" label="图标" min-width="70" sortable>
        <template scope="scope">
          <img :src="imgPrefix+scope.row.logo" width="50px">
        </template>
      </el-table-column>
      <el-table-column prop="admin.username" label="管理员" min-width="75" sortable>
      </el-table-column>
      <el-table-column label="操作" width="220">
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
          <el-button type="warning" size="small" :disabled="scope.row.state!=1" @click="handleAudit(scope.$index, scope.row)">审核</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="pageSize"
                     :total="totals" :current-page="currentPage" style="float:right;">
      </el-pagination>
    </el-col>

    <!--编辑界面-->
    <el-dialog :title="title" :visible.sync="saveFormVisible" :close-on-click-modal="false">
      <el-form :model="saveForm" label-width="80px" ref="saveForm">
        <el-form-item label="店铺名">
          <el-input v-model="saveForm.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="店铺电话">
          <el-input v-model="saveForm.tel" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="saveForm.state" placeholder="请选择">
            <el-option
                v-for="item in stateOptions"
                :label="item.label"
                :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="管理员">
          <el-select v-model="saveForm.admin_id" placeholder="请选择">
            <el-option
                v-for="item in employees"
                :label="item.username"
                :value="item.id">
              <span style="float: left">{{ item.username }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{ item.phone }}</span>
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="saveFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="saveSubmit" :loading="saveLoading">提交</el-button>
      </div>
    </el-dialog>

    <!-- 审核模态框 -->
    <el-dialog title="审核店铺" :visible.sync="shopAuditVisible" :close-on-click-modal="false">
      <el-form :model="shopAuditLog" label-width="80px"  ref="shopAuditLogForm">
        <el-form-item label="备注" prop="note">
          <el-input type="textarea" v-model="shopAuditLog.note"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="shopAuditVisible = false">取消</el-button>
        <el-button type="primary" @click.native="auditPass">通过</el-button>
        <el-button type="primary" @click.native="auditReject">驳回</el-button>
      </div>
    </el-dialog>
  </section>
</template>

<script>

export default {
  data() {
    return {
      imgPrefix: 'http://123.207.27.208',
      filters: {
        keywords: ''
      },
      shops: [],
      totals: 0,
      currentPage: 1,
      pageSize: 2,
      listLoading: false,
      sels: [],//列表选中列
      employees: [],
      saveFormVisible: false,//编辑界面是否显示
      shopAuditVisible: false, // 审核界面是否显示
      saveLoading: false,

      //编辑界面数据
      saveForm: {
        id: null,
        name: '',
        tel: '',
        registerTime: null,
        state: null,
        address: '',
        logo: '',
        admin_id: null,
      },
      title: '',
      stateOptions: [{
        value: 1,
        label: '待审核'
      }, {
        value: 2,
        label: '审核中'
      }, {
        value: 3,
        label: '通过'
      }, {
        value: 4,
        label: '驳回'
      }],
      shopAuditLog: {
        shop_id: null,
        note: '',
      }
    }
  },
  methods: {
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getShops();
    },
    //获取用户列表
    getShops() {
      let para = {
        pageSize: this.pageSize,
        currentPage: this.currentPage,
        keywords: this.filters.keywords
      };
      this.listLoading = true;
      this.$http.post("/shop", para).then(result => {
        this.totals = result.data.totals;
        this.shops = result.data.data;
        this.listLoading = false;
      }).catch(result => {
        this.$message.error('请求错误');
      })
    },
    keywordsQuery() {
      this.currentPage = 1;
      this.getShops();
    },
    getEmployees(shopId) {
      if (shopId) {
        this.$http.get("/employee/shopemp/" + shopId).then(result => {
              this.employees = result.data;
            }
        )
      } else {
        this.$http.get("/employee").then(result => {
              this.employees = result.data;
            }
        )
      }

    },
    //删除
    handleDel: function (index, row) {
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        this.$http.delete("/shop/" + row.id).then(result => {
          this.$message(result.data.msg);
        }).catch(res => {
          this.$message({
            message: '系统错误，稍后尝试',
            type: 'error'
          });
        })
        this.currentPage = 1;
        this.getShops();
        this.listLoading = false;
      }).catch(() => {
      });
    },
    //显示编辑界面
    handleEdit: function (index, row) {
      console.debug(row.id);
      this.getEmployees(row.id);
      this.title = '修改';
      this.saveFormVisible = true;
      this.saveForm = Object.assign({}, row);
    },
    //显示新增界面
    handleAdd: function () {
      this.getEmployees();
      this.title = '新增';
      this.saveFormVisible = true;
      this.saveForm = {
        id: null,
        name: '',
        tel: '',
        registerTime: null,
        state: null,
        address: '',
        logo: '',
        admin_id: null
      };
    },
    // 显示审核模态框
    handleAudit(index, row){
      this.shopAuditLog.shop_id = row.id;
      this.shopAuditLog.note = '';
      this.shopAuditVisible = true;
    },
    //审核驳回
    auditReject(){
      this.$http.post("/shop/audit/reject",this.shopAuditLog).then(res=>{
        if(res.data.success){
          this.$message({
            message: '驳回成功',
            type: 'success'
          });
        }else{
          this.$message({
            message: '驳回失败',
            type: 'error'
          });
        }
        this.shopAuditVisible = false;
        this.getShops();
      })
    },
    //审核通过
    auditPass(){
      this.$http.post("/shop/audit/pass",this.shopAuditLog).then(result=>{
        result = result.data;
        if(result.success){
          this.$message({
            message: '审核通过',
            type: 'success'
          });
        }else{
          this.$message({
            message: '审核失败',
            type: 'error'
          });
        }
        this.shopAuditVisible = false;
        this.getShops();
      })
    },
    //编辑
    saveSubmit: function () {
      this.$refs.saveForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            this.saveLoading = true;
            let para = Object.assign({}, this.saveForm);
            this.$http.put("/shop", para).then(result=>{
              this.$message(result.data.msg);
            }).catch(result=>{
              this.$message({
                message: '系统错误，稍后尝试',
                type: 'error'
              });
            })
            this.saveLoading = false;
            this.saveFormVisible = false;
            this.getShops();
          });
        }
      });
    },
    selsChange: function (sels) {
      this.sels = sels;
    },
    //批量删除
    batchRemove: function () {
      var ids = this.sels.map(item => item.id).toString();
      this.$confirm('确认删除选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        let ids = this.sels.map(item => item.id);
        this.$http.patch("/shop", ids).then(result => {
          this.$message(result.data.msg);
        }).catch(res => {
          this.$message({
            message: '系统错误，稍后尝试',
            type: 'error'
          });
        })
        this.currentPage = 1;
        this.getShops();
        this.listLoading = false;
      }).catch(() => {
      });
    },
    handleExport(){
      location.href="http://localhost:8080/shop/export";
    }
  },
  mounted() {
    this.getShops();
  }
}

</script>

<style scoped>

</style>